package com.taomus.mytools.simplec.syntax;

import java.util.Queue;
import java.util.Stack;

import com.taomus.mytools.simplec.common.SyntaxTuple;
import com.taomus.mytools.simplec.common.SyntaxTupleList;
import com.taomus.mytools.simplec.common.Token;
import com.taomus.mytools.simplec.common.Word;
import com.taomus.mytools.simplec.common.WordEnum;
import com.taomus.mytools.simplec.utils.ErrorUtils;
import com.taomus.mytools.simplec.utils.Q;

/**
 * 解析符号是否符合 变量定义
 * 
 * @author user
 *
 */
public class Variable implements IAnalyse {
	private Q<Token> tkQ;
	private Stack<Token> stack = new Stack<>();

	public Variable(final Queue<Token> inTkQueue) {
		this.tkQ = new Q<Token>(inTkQueue);
	}

	@Override
	public boolean complier() {
		return block();
	}

	private boolean block() {
		Token t1 = tkQ.peek();
		//System.err.println(" 37 " + t1);
		if(t1.getCode() == WordEnum.IFEND){
			//?.
			SyntaxTupleList.add(new SyntaxTuple(tkQ.poll()));
			return true;
		}else if(t1.getCode() == WordEnum.IFELSE){
			//!
			SyntaxTupleList.add(new SyntaxTuple(tkQ.poll()));
			return true;
		}else if (t1.getCode() == WordEnum.FUNCR) {
			// }
			SyntaxTupleList.add(new SyntaxTuple(tkQ.poll()));
			return true;
		} else if (t1.getCode() == WordEnum.ID) {
			this.stack.push(tkQ.poll());
			Token op = tkQ.peek();
//			System.err.println(" 45 " + op);
			switch (op.getCode()) {
			case IFELSE:
			case IFEND:
			case FUNCR:
				SyntaxTupleList.add(new SyntaxTuple(WordEnum.FRET,this.stack.pop()));
				return true;
			case LP:
				tkQ.poll();
				FAGRS agrs  = new FAGRS(tkQ, stack);
				if(!agrs.complier()){
					return false;
				}
				SyntaxTupleList.add(new SyntaxTuple(Word.TK_FCALL,stack.pop()));
				return true;
			case MOV:
				op = tkQ.poll();
		//		System.err.println(" 49 " + op);
				Token temp = tkQ.peek();
			//	System.err.println(" 51 " + temp);
				switch (temp.getCode()) {
				case LP:
					// (
					this.stack.push(tkQ.poll());
					Operation operation2 = new Operation(tkQ, stack);
					if (!operation2.complier()) {
						return false;
					}
					break;
				case ID:
				case DECIMAL:
				case NUMBER:
				case CHAR:
				case STRING:
				case FAGRS:
					// ID
					this.stack.push(tkQ.poll());
					temp = tkQ.peek();
				//	System.err.println(" 71 " + temp);
				//	System.err.println(" 71 " + temp.getCode().getCode());
					if (temp.getCode().getCode() >= WordEnum.ADD.getCode()
							&& temp.getCode().getCode() <= WordEnum.MOD.getCode()) {
						Operation operation = new Operation(tkQ, stack);
						if (!operation.complier()) {
							return false;
						}
					}
					break;
				case FUNCL:
					// {
					this.stack.push(tkQ.poll());
					break;
				default:
					ErrorUtils.error("variable error");
					break;
				}
				SyntaxTuple st = new SyntaxTuple();
				st.opcode(op);
				st.t3(this.stack.pop());
				st.t1(this.stack.pop());
				SyntaxTupleList.add(st);
				return true;
			default:
				temp = tkQ.peek();
				if (temp.getCode().getCode() >= WordEnum.EQ.getCode()
						&& temp.getCode().getCode() <= WordEnum.LE.getCode()) {
					If _if = new If(tkQ, stack);
					if (!_if.complier()) {
						return false;
					}
					Token isIf = this.tkQ.peek();
					if (isIf.getCode() == WordEnum.IF) {
						this.tkQ.poll();
						SyntaxTupleList.add(new SyntaxTuple(Word.KW_IF, this.stack.pop()));
						return true;
					}
				}
				break;
			}
		}
		return false;
	}
}
